package cn.web.tunnelrobot.service.impl;

import cn.web.tunnelrobot.StartService;
import cn.web.tunnelrobot.dao.*;
import cn.web.tunnelrobot.dto.Project1;
import cn.web.tunnelrobot.dto.ProjectDTO;
import cn.web.tunnelrobot.pojo.*;
import cn.web.tunnelrobot.service.ProjectService;
import cn.web.tunnelrobot.util.Constants;
import cn.web.tunnelrobot.util.EntityUtils;
import cn.web.tunnelrobot.util.MileageConversion;
import cn.web.tunnelrobot.util.PlanProgress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectDao projectDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRecordDao userRecordDao;
    @Autowired
    private UnitInfoDao unitInfoDao;
    @Autowired
    private WorkAreaDao workAreaDao;
    @Override
    public HashMap<String, Object> addProject(Project1 project) {
        /*隧道总长度*/
        Float length=(float)((project.getD2()*1000+project.getD2_1())-((project.getD1())*1000+project.getD1_1()));
        project.setPrTunnelLength(length);
        /*开挖进度*/
        Float kwp=(float)((project.getD7()*1000+project.getD7_1())-((project.getD1())*1000+project.getD1_1()))/project.getPrTunnelLength();
        project.setKwProgress((float)Math.round(kwp*1000)/10);

        /*初支进度*/
        Float czp=(float)((project.getD5()*1000+project.getD5_1())-((project.getD1())*1000+project.getD1_1()))/project.getPrTunnelLength();
        project.setCzProgress((float)Math.round(czp*1000)/10);

        /*二衬进度*/
        Float ecp=(float)((project.getD3()*1000+project.getD3_1())-((project.getD1())*1000+project.getD1_1()))/project.getPrTunnelLength();
        project.setEcProgress((float)Math.round(ecp*1000)/10);

        /*开挖距离掌子面*/
        Float kwh=(float)0;
        project.setKwHeadingFace(kwh);

        /*初支距离掌子面*/
        Float czh=(float)((project.getD8()*1000+project.getD8_1())-((project.getD6())*1000+project.getD6_1()));
        project.setCzHeadingFace(czh);

        /*二衬距离掌子面*/
        Float ech=(float)((project.getD8()*1000+project.getD8_1())-((project.getD4())*1000+project.getD4_1()));
        project.setEcHeadingFace(ech);

        /*初始里程*/
        Float initial=(float)((project.getD1())*1000+project.getD1_1());
        project.setInitial(initial);

        /*尾部里程*/
        Float tail=(float)((project.getD2())*1000+project.getD2_1());
        project.setTail(tail);

        /*开挖初始里程*/
        Float kw_initial=(float)((project.getD7())*1000+project.getD7_1());
        project.setKwInitial(kw_initial);

        /*开挖尾部里程*/
        Float kw_tail=(float)(project.getD8()*1000+project.getD8_1());
        project.setKwTail(kw_tail);

        /*初支初始里程*/
        Float cz_initial=(float)((project.getD5())*1000+project.getD5_1());
        project.setCzInitial(cz_initial);

        /*初支尾部里程*/
        Float cz_tail=(float)(project.getD6()*1000+project.getD6_1());
        project.setCzTail(cz_tail);

        /*二衬初始里程*/
        Float ec_initial=(float)((project.getD3())*1000+project.getD3_1());
        project.setEcInitial(ec_initial);

        /*二衬尾部里程*/
        Float ec_tail=(float)(project.getD4()*1000+project.getD4_1());
        project.setEcTail(ec_tail);
        /************/


        PlanProgress p=new PlanProgress();
        String kwplan=p.planProgress((project.getD7()*1000+project.getD7_1())-((project.getD13())*1000+project.getD13_1()));
        project.setKwPlanProgress(kwplan);
        String czplan=p.planProgress((project.getD5()*1000+project.getD5_1())-((project.getD11())*1000+project.getD11_1()));
        project.setCzPlanProgress(czplan);
        String ecplan=p.planProgress((project.getD3()*1000+project.getD3_1())-((project.getD9())*1000+project.getD9_1()));
        project.setEcPlanProgress(ecplan);

        HashMap<String,Object> map = new HashMap<String,Object>();
       /* if(Constants.is_intranet == 1){
            //内网不提供编辑权限
            map.put("code",-1);
            map.put("msg","内网没有权限");
            return map;
        }*/
        User user = new User();
        if (project.getOperator() == null){
            map.put("code",-1);
            map.put("msg","操作人不能为空");
            return map;
        }
       /* Project projectInfo = projectDao.getProject();
        if (projectInfo != null){
            map.put("code",-1);
            map.put("msg","不能添加，请去修改");
            return map;
        }*/
        user.setUsId(project.getOperator());
        User user1 = userDao.getUserInfo(user);
        if (user1.getUsRole() == 2){
            map.put("code",-1);
            map.put("msg","暂无权限");
            return map;
        }
        WorkArea workArea1 = workAreaDao.getWorkAreaByUsId(project.getUsId());
        if (workArea1 != null){
            map.put("code",-1);
            map.put("msg","该用户已有负责标段");
            return map;
        }
        UnitInfo unitInfo1 = unitInfoDao.getUnitInfoByUsId(project.getUsId());
        if (unitInfo1 != null){
            map.put("code",-1);
            map.put("msg","该用户已有负责施工单位");
            return map;
        }
        Project project2 = projectDao.getProjectByUsId(project.getUsId());
        if (project2 != null){
            map.put("code",-1);
            map.put("msg","该用户已有负责项目");
            return map;
        }
        project.setPrCreateDate(new Date());
        Project project1 = projectDao.getProjectByName(project.getUiId(),project.getPrName());
        if (project1 != null){
            map.put("msg","项目名已存在，请勿重复添加");
            map.put("code",-1);
            return map;
        }
        int result = projectDao.addProject(project);
        if (result == 1){
            map.put("msg","添加成功");
            map.put("code",0);
           /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user2.getUsId());
            userRecord.setUrOperatContent("添加项目信息");
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
        }else {
            map.put("code",-1);
            map.put("msg","添加失败");
        }
        return map;
    }
    @Override
    public HashMap<String, Object> updateProject(Project1 project) {
        Project project1 = projectDao.getProjectById(project.getPrId());
        project1.setPrId(project.getPrId());
        if(project.getD1()!=null){
            /*隧道总长度*/
            Float length=(float)((project.getD2()*1000+project.getD2_1())-((project.getD1())*1000+project.getD1_1()));
            project1.setPrTunnelLength(length);
            /*开挖进度*/
            Float kwp=(float)((project.getD7()*1000+project.getD7_1())-((project.getD1())*1000+project.getD1_1()))/project1.getPrTunnelLength();
            project1.setKwProgress((float)Math.round(kwp*1000)/10);

            /*初支进度*/
            Float czp=(float)((project.getD5()*1000+project.getD5_1())-((project.getD1())*1000+project.getD1_1()))/project1.getPrTunnelLength();
            project1.setCzProgress((float)Math.round(czp*1000)/10);

            /*二衬进度*/
            Float ecp=(float)((project.getD3()*1000+project.getD3_1())-((project.getD1())*1000+project.getD1_1()))/project1.getPrTunnelLength();
            project1.setEcProgress((float)Math.round(ecp*1000)/10);
            /*初始里程*/
            Float initial=(float)((project.getD1())*1000+project.getD1_1());
            project1.setInitial(initial);

            /*尾部里程*/
            Float tail=(float)((project.getD2())*1000+project.getD2_1());
            project1.setTail(tail);
            project1.setPrIntro(project.getPrIntro());
            project1.setPrName(project.getPrName());
            project1.setLine(project.getLine());
            project1.setPrCreateDate(new Date());
            project1.setUiId(null);
            if(project.getPrId() == 1){
                if(!project.getPrIntro().equals(project1.getPrIntro())){
                    ProjectDTO projectDTO = EntityUtils.copyObject(project, ProjectDTO.class);
                    StartService.projectDTO = projectDTO ;
                }
            }
        }


        /*开挖距离掌子面*/
        Float kwh=(float)0;
        project1.setKwHeadingFace(kwh);

        /*初支距离掌子面*/
        Float czh=(float)((project.getD8()*1000+project.getD8_1())-((project.getD6())*1000+project.getD6_1()));
        project1.setCzHeadingFace(czh);

        /*二衬距离掌子面*/
        Float ech=(float)((project.getD8()*1000+project.getD8_1())-((project.getD4())*1000+project.getD4_1()));
        project1.setEcHeadingFace(ech);



        /*开挖初始里程*/
        Float kw_initial=(float)((project.getD7())*1000+project.getD7_1());
        project1.setKwInitial(kw_initial);

        /*开挖尾部里程*/
        Float kw_tail=(float)(project.getD8()*1000+project.getD8_1());
        project1.setKwTail(kw_tail);

        /*初支初始里程*/
        Float cz_initial=(float)((project.getD5())*1000+project.getD5_1());
        project1.setCzInitial(cz_initial);

        /*初支尾部里程*/
        Float cz_tail=(float)(project.getD6()*1000+project.getD6_1());
        project1.setCzTail(cz_tail);

        /*二衬初始里程*/
        Float ec_initial=(float)((project.getD3())*1000+project.getD3_1());
        project1.setEcInitial(ec_initial);

        /*二衬尾部里程*/
        Float ec_tail=(float)(project.getD4()*1000+project.getD4_1());
        project1.setEcTail(ec_tail);


        PlanProgress p=new PlanProgress();
        String kwplan=p.planProgress(((project.getD7()*1000+project.getD7_1())-((project.getD13())*1000+project.getD13_1())));
        project1.setKwPlanProgress(kwplan);
        String czplan=p.planProgress(((project.getD5()*1000+project.getD5_1())-((project.getD11())*1000+project.getD11_1())));
        project1.setCzPlanProgress(czplan);
        String ecplan=p.planProgress(((project.getD3()*1000+project.getD3_1())-((project.getD9())*1000+project.getD9_1())));
        project1.setEcPlanProgress(ecplan);

        HashMap<String,Object> map = new HashMap<String,Object>();
     /*   if(Constants.is_intranet == 1){
            //内网不提供编辑权限
            map.put("code",-1);
            map.put("msg","内网没有权限");
            return map;
        }*/
        User user = new User();
        user.setUsId(project.getOperator());
        User user1 = userDao.getUserInfo(user);
        if (user1.getUsRole() == 2){
            map.put("code",-1);
            map.put("msg","暂无权限");
            return map;
        }

        if (project.getUsId() != project1.getUsId()){
            WorkArea workArea1 = workAreaDao.getWorkAreaByUsId(project.getUsId());
            if (workArea1 != null){
                map.put("code",-1);
                map.put("msg","该用户已有负责标段");
                return map;
            }
            UnitInfo unitInfo1 = unitInfoDao.getUnitInfoByUsId(project.getUsId());
            if (unitInfo1 != null){
                map.put("code",-1);
                map.put("msg","该用户已有负责施工单位");
                return map;
            }
            Project project3 = projectDao.getProjectByUsId(project.getUsId());
            if (project3 != null){
                map.put("code",-1);
                map.put("msg","该用户已有负责项目");
                return map;
            }
        }


        int result = projectDao.updateProject(project1);
        if (result == 1){
            map.put("msg","修改成功");
            map.put("code",0);
            StartService.projectDTO = projectDao.getDetailProjectById(1);//更新项目信息
          /*  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user2.getUsId());
            userRecord.setUrOperatContent("修改项目信息");
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
        }else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }
        return map;
    }
    @Override
    public HashMap<String, Object> getProjectList(Integer uiId) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        List<Project> list = new ArrayList<>();
        try {
            list = projectDao.getProjectList(uiId);
            if (list.size() > 0){
                map.put("code",0);
                map.put("data",list);
               /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
                UserRecord userRecord = new UserRecord();
                userRecord.setUsId(user2.getUsId());
                userRecord.setUrOperatContent("查询项目信息列表");
                userRecord.setUrOperatTime(new Date());
                userRecordDao.addUserRecord(userRecord);*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    @Override
    public HashMap<String, Object> getProjectByIdNew(Integer prId) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        Project projectDTO = projectDao.getProjectById(prId);
        if (projectDTO != null){
            String kwI=MileageConversion.mileage(projectDTO.getKwInitial());
            String kwT=MileageConversion.mileage(projectDTO.getKwTail());
            String czI=MileageConversion.mileage(projectDTO.getCzInitial());
            String ecI=MileageConversion.mileage(projectDTO.getEcInitial());
            projectDTO.setKwDK(kwI+"-"+kwT);
            projectDTO.setCzDK(czI+"-"+kwI);
            projectDTO.setEcDK(ecI+"-"+czI);
            map.put("code",0);
            map.put("data",projectDTO);
           /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user2.getUsId());
            userRecord.setUrOperatContent("查询项目信息详情");
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
        }else {
            map.put("code",-1);
            map.put("msg","删除失败");
        }
        return map;
    }
    @Override
    public HashMap<String, Object> getProjectById(Integer prId) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        ProjectDTO projectDTO = projectDao.getDetailProjectById(prId);
        if (projectDTO != null){
            map.put("code",0);
            map.put("data",projectDTO);
           /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user2.getUsId());
            userRecord.setUrOperatContent("查询项目信息详情");
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
        }else {
            map.put("code",-1);
            map.put("msg","删除失败");
        }
        return map;
    }
    @Override
    public HashMap<String, Object> deleteProjectByPrId(Integer prId){
        HashMap<String, Object> map=new HashMap<>();
        List<WorkArea> list = workAreaDao.getWorkAreaList(prId);
        if(list.size()>0){
            map.put("code",-1);
            map.put("msg","该项目下有工段，请先删除工段");
        }else {
            int result=projectDao.deleteProjectByPrId(prId);
            if(result==1){
                map.put("code",0);
                map.put("msg","删除成功");
            }else {
                map.put("code",-1);
                map.put("msg","删除失败");
            }
        }
        return map;
    }

}
